<template>
  <div>
    <div class="main">
      <div class="sczl">
        <div
          style="
            display: flex;
            justify-content: space-between;
            align-items: center;
          "
        >
          <div class="content-title">上传资料</div>
          <!-- <div style="display: flex; align-items: center; cursor: pointer">
            <img class="zcImg" src="@/assets/zc.png" width="16" alt="" />
            <div class="zcText">暂存</div>
          </div> -->
        </div>
        <el-divider></el-divider>
        <div class="form-title">
          <!-- <span class="form-title-tit" style="font-weight: 700;">提醒：</span> -->
          <span
            ><span style="font-weight: 700">提醒：</span>
            <img
              class=""
              src="@/assets/hx.png"
              width="14"
              alt=""
            />为必需材料，您必须提交才能申报，
            <img
              class=""
              src="@/assets/bx.png"
              width="14"
              alt=""
            />为非必需材料，根据您实际情况选择提交
          </span>
        </div>
        <div style="display: flex;justify-content: space-between;padding: 3px 0px;">
          <p class="stuff">
            <img class="" src="@/assets/sbcl.png" width="22" alt="" />
            <span>申报材料—上传</span>
          </p>
          <div class="project-status" v-if="currentProject && currentProject.pushFlag == '1'">
            已提交
          </div>
        </div>
        <!-- <el-divider></el-divider> -->
        <div>
          <el-select
            filterable
            size="small"
            style="width: 100%"
            v-model="currentProjectId"
            @change="projectChange"
            placeholder="请选择项目"
          >
            <el-option
              v-for="(item, idx) in projectList"
              :key="idx"
              :label="item.projectName"
              :value="item.id"
            >
              <span style="float: left">{{ item.projectName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.projectNo}}</span> 
            </el-option>
          </el-select>
        </div>
        <el-divider></el-divider>
        <el-table
          :data="tableData"
          style="width: 100%"
          :header-cell-style="{ background: '#f6f6f6', color: '#333' }"
          :row-class-name="rowClassName"
        >
          <el-table-column
            label="序号"
            align="center"
            prop="xh"
            width="50"
          ></el-table-column>
          <el-table-column prop="dictLabel" label="材料名称" width="280">
            <template slot-scope="scope">
              <div>
                <span
                  style="margin-right: 5px"
                  v-if="scope.row.needFlag == '0'"
                  ><img src="@/assets/bx.png" alt="" width="12"
                /></span>
                <!-- <span
                  style="margin-right: 5px"
                  v-else-if="scope.row.clmc == '违法整改征转用批复'"
                  ><img src="@/assets/bx.png" alt="" width="12"
                /></span> -->
                <span style="margin-right: 5px" v-else
                  ><img src="@/assets/hx.png" alt="" width="12"
                /></span>
                <span>{{ scope.row.dictLabel }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="ybxz" label="样表下载">
            <template slot-scope="scope">
              <el-button type="text" 
               style="color: #0c7cff;"
              :disabled='scope.row.templateId == null' 
              @click="download(scope.row)"
              >下载</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="files" label="文件名">
            <template slot-scope="scope">
              <div v-if="scope.row.files.length>0" class="file-list">
                <span class="file-item" v-for="(item,idx) in scope.row.files" :key="'files'+idx">
                  <span class="file-item-text">{{item.fileName}}</span>
                  <el-popconfirm
                    title="确定删除吗？"
                    @confirm="deleteFile(item)"
                  >
                    <i class="el-icon-circle-close" style="color:red;" slot="reference"></i>
                  </el-popconfirm>
                </span>
              </div>
              <div v-else style="color:#999999">
                暂无文件
              </div>     
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="uploadZl(scope.row)"  style="color: #0c7cff;"
                >上传材料</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-divider></el-divider>
        <div style="display: flex;justify-content: space-between;padding: 3px 0px;">
          <p class="stuff">
            <img class="" src="@/assets/sbcl.png" width="22" alt="" />
            <span>用地审批资料—查看</span>
          </p>
        </div>
        <el-table
          :data="tableTwoData"
          style="width: 100%"
        >
          <el-table-column prop="dictLabel" label="材料名称">
            <template slot-scope="scope">
              <span class="file-item-text">{{scope.row.dictLabel}}</span>
            </template>
          </el-table-column>
          <el-table-column label="文件" align="right">
            <template slot-scope="scope">
              <div v-if="scope.row.files">
                <div class="file-sub-item" v-for="(item,idx) in scope.row.files" :key="idx">
                  <span class="file-sub-title">{{item.fileName }}</span>
                  <el-button type="text" v-loading.fullscreen.lock="fullscreenLoading"  @click="downloadTwo(item)">下载</el-button> 
                </div>
              </div>
              <div v-else>无</div>
            </template>
          </el-table-column>
        </el-table>
        <el-footer style="text-align: center; margin-top: 10px">
          <el-button type="info" @click="goBack">上一步</el-button>
          <el-button type="primary" @click="goNext">下一步</el-button>
        </el-footer>
      </div>
    </div>
    <UploadDialog ref="UploadDialog" @success="getProjectFile" />
  </div>
</template>

<script>
import service from "@/service/apiService";
import UploadDialog from "./UploadDialog.vue";

export default {
  data() {
    return {
      projectList: [],
      currentProjectId: null,
      currentProject:null,
      userInfo: {},
      tableData: [],
      tableTwoData:[],
      fullscreenLoading:false,
    };
  },
  components: {
    UploadDialog,
  },
  mounted() {
    let user = window.localStorage.getItem("userInfo");
    this.userInfo = JSON.parse(user);
    this.loadData();
    this.getFileTypeData()
    this.getTotalFileType()
  },
  methods: {
    //获取附件类型-旧
    loadData() {
      service
        .getProjectListData({
          currentPage: 1,
          pageSize: 1000,
          query: {
            telephone: this.userInfo.telephone,
          },
        })
        .then((res) => {
          this.projectList = res.data.records;
        })
        .catch((err) => {
          console.error(err)
        });
    },
    getFileTypeData(){
      service.getDictData('yysq-fjlx').then(res=>{
        this.tableData = res.data
        this.tableData.forEach(item=>{
          item.files = []
        })

      }).catch(err=>{

      })
    },
    //获取附件类型-新
    getTotalFileType(){
      service.getDictData('sltzs-fjlx').then(res=>{
        this.tableTwoData = res.data
      }).catch(err=>{

      })
    },
    deleteFile(val) {
      service
        .deleteFileForId(val.id)
        .then((res) => {
          this.getProjectFile()
        })
        .catch((err) => {
          console.error(err)
        });
    },
    getProjectFile() {
      let self = this
      let tempData = []
      this.tableData.forEach(item=>{
        tempData.push(item)
      })
      this.tableData = []
      service
        .getProjectFileList({
          ywbh: this.currentProject.id,
          fileTypes: [
            "fj_yssq","fj_tdfkfa","fj_qhdbzp","fj_jhtd","fj_jgzl","fj_zfdfk","fj_wfzgzzypf","fj_zbcwj","fj_qt",
          ],
        })
        .then((res) => {
          let data = res.data
          tempData.forEach(item=>{
            item.files = data[item.dictcode]
          })
          self.tableData = tempData
        });
      this.getReturnFileData()
    },
    getReturnFileData(){
      let self = this
      let tempData = []
      this.tableTwoData.forEach(item=>{
        tempData.push(item)
      })
      this.tableTwoData = []
      service
        .getProjectFileList({
          ywbh: this.currentProject.id,
          fileTypes: [
            'lsydtdht','lsydsqb','sltzstdlyxzzp','lqcgb','kcdjt','tdkcdjjsbgs','fkbgbjscyj','xmyjwj','yctdfkfypz'
          ],
        })
        .then((res) => {
          let data = res.data
          tempData.forEach(item=>{
            item.files = data[item.dictcode]
          })
          self.tableTwoData = tempData
        });
    },
    projectChange(val) {
      this.currentProject = this.projectList.find(item=> item.id == this.currentProjectId)
      window.localStorage.setItem('currentProject',JSON.stringify(this.currentProject))
      this.getProjectFile();
    },
    uploadZl(val) {
      if(!this.currentProjectId){
        this.$message.warning('请先选择申报材料项目')
      }else{
        this.$refs.UploadDialog.show(val.dictcode,this.currentProject.id,val.templateSuffix);
      }
    },
    download(val) {
      service
        .downloadFile({
          annexId: val.templateId,
        })
        .then((res) => {
          const link = document.createElement("a");
          let blob = res.data; //如果后台返回的直接是blob对象类型，直接获取数据
          // let _fileName = res.headers['content-disposition'].split(';')[1].split('=')[1]; //拆解获取文件名，
          link.style.display = "none";
          const url = window.URL || window.webkitURL || window.moxURL;
          link.href = window.URL.createObjectURL(blob);
          link.download = val.dictLabel + val.templateSuffix; //下载的文件名称
          link.click();
          window.URL.revokeObjectURL(url);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    downloadTwo(val) {
      this.fullscreenLoading = true
      service
        .downloadFile({
          annexId: val.id,
        })
        .then((res) => {
          this.fullscreenLoading = false
          const link = document.createElement("a");
          let blob = res.data; //如果后台返回的直接是blob对象类型，直接获取数据
          link.style.display = "none";
          const url = window.URL || window.webkitURL || window.moxURL;
          link.href = window.URL.createObjectURL(blob);
          link.download = val.fileName; //下载的文件名称
          link.click();
          window.URL.revokeObjectURL(url);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    goNext() {
      if (!this.currentProject) {
        this.$message({
          type: 'warning',
          message: '请选择项目!'
        });
        return
      }
      for (let i = 0; i < this.tableData.length; i++) {
        const item = this.tableData[i];
        if (item.needFlag == '1' && item.files.length == 0) {
          this.$message({
            type: 'warning',
            message: '请上传'+item.dictLabel
          });
          return
        }
      }

      this.$emit("goStep");
    },
    goBack() {
      this.$emit("goBack");
    },
    rowClassName({ row, rowIndex }) {
      row.xh = rowIndex + 1;
      if (row.xh < 10) {
        row.xh = "0" + row.xh;
      }
    },
  },
};
</script>

<style scoped lang="less">
.file-list{
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.file-item{
  display: flex;
  align-items: center;
  justify-content: center;
}
.file-item-text{
  margin-right: 5px;
}
.demo-ruleForm {
  padding: 20px 15px;
}

.stuff {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: 700;
  color: #0c7cff;
  padding-bottom: 5px;
}

.zcImg {
  margin-right: 5px;
}

.zcText {
  font-size: 16px;
  color: #0c7cff;
}

.form-title {
  margin: 17px 0;
  width: 100%;
  height: 54px;
  background: #f6f6f6;
  border: 1px solid #f6f6f6;
  box-sizing: border-box;
  padding: 0 30px;
  display: flex;
  align-items: center;
  font-size: 16px;
  color: #333;
}

.content-title {
  font-size: 24px;
  color: #222;
  font-weight: 700;
}
.project-status{
  background-color: #0c7cff;
  color: #f6f6f6;
  font-size: 14px;
  padding: 3px 10px;
  text-align: center;
  display: flex;
  align-items: center;
  border-radius: 10px;
}
.file-sub-item{
  display: flex;
  align-items: center;
  justify-content: space-between;
  .file-sub-title{
    white-space: nowrap;
    max-width: 90%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

/deep/ .el-button--primary{
  background-color: #0c7cff !important;
}

</style>